<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/2/20
 * Time: 16:46
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Verify extends Base
{
    //----------------------------------------------------------------------------------------------------
    //------------------------------------医生审核----------------------------------------------------
    //----------------------------------------------------------------------------------------------------
    /**
     * 审核医生列表
     * @return mixed|\think\response\Json
     */
    public function user(){
        if(request()->isAjax()){
            extract(input());
            $where = [
                'user_status' => ['neq', 3],
                'user_type' => 1
            ];
            if(isset($keyword) && $keyword != ''){
                $where['user_phone|user_username'] = ['like', '%' . $keyword . '%'];
            }
            if(isset($user_verify) && $user_verify != "")
            {
                $where['user_verify'] = $user_verify;
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('u_user')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('u_user')
                ->where($where)
                ->page($NowPage,$limits)
                ->order('user_verify asc,user_time asc')
                ->select();
            $iCan = session('uid');
            foreach ($lists as $key => $vo){
//                省市
                $area = [$vo['user_sheng2'],$vo['user_shi2']];
                $ss = Db::table('up_area')
                    ->where('district_id','in',$area)
                    ->column('district');
                $lists[$key]['areastr'] = implode($ss,'-');
//                科室-职位
                $lists[$key]['room_job'] = $vo['user_room'].'-'.$vo['user_job_desc'];
                $lists[$key]['iCan'] = $iCan;
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    /**
     * 编辑用户
     * @return mixed|\think\response\Json
     */
    public function useredit(){
        if(Request::instance()->isPost()){
            extract(input());
            $today = date('Y-m-d H:i:s');
            $update = [
                'user_verify' => 2,
                'user_can' => 2,
                'user_username' => $user_username,
                'user_image' => $user_image,
                'user_sex' => $user_sex,
                'user_edu' => $user_edu,
                'user_birthday' => $user_birthday,
                'user_card' => $user_card,

                'user_sheng' => $user_sheng2,
                'user_shi' => $user_shi2,
                'user_hospital' => $user_hospital2,
                'user_room' => $user_room2,
                'user_job_desc' => $user_job_desc2,
                'user_sheng2' => $user_sheng2,
                'user_shi2' => $user_shi2,
                'user_hospital2' => $user_hospital2,
                'user_room2' => $user_room2,
                'user_job_desc2' => $user_job_desc2,
                'user_verify_time' => $today
            ];
            if($type == 1){
                $update['user_verify'] = 2;
                $update['user_can'] = 2;
            }
            $res = Db::table('up_u_user')->where('user_id', $user_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $user_id = input('user_id');
        $type = input('type');
        $info = Db::table('up_u_user')
            ->where('user_id',$user_id)
            ->find();
        $province = Db::table('up_area')
            ->field('district_id,district')
            ->where('pid',0)
            ->select();
        $city = Db::table('up_area')
            ->field('district_id,district')
            ->where('pid',$info['user_sheng2'])
            ->select();
        $this->assign([
            'info' => $info,
            'type' => $type,
            'province' => $province,
            'city' => $city,
        ]);
        return $this->fetch();
    }
    /**
     * 用户同意
     * @return \think\response\Json
     */
    public function useragree(){
        $user_id = input('id');
        $user = Db::table('up_u_user')->where('user_id', $user_id)->find();
        $update = [
            'user_verify' => 2,
            'user_can' => 2,
            'user_sheng' => $user['user_sheng2'],
            'user_shi' => $user['user_shi2'],
            'user_hospital' => $user['user_hospital2'],
            'user_room' => $user['user_room2'],
            'user_job_desc' => $user['user_job_desc2'],
            'user_verify_time' => date('Y-m-d H:i:s')
        ];
        $have = Db::table('up_hospital')->where('hos_name', $user['user_hospital2'])->find();
        if(!$have) return json(['code' => 100, 'msg' => '医院库无此医院']);
        $res = Db::table('up_u_user')->where('user_id', $user_id)->update($update);
        if($res){
            try {
                YzxSms('590520','',$user['user_phone'],'');
            } catch (\Exception $e) {
            }
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 用户拒绝
     * @return \think\response\Json
     */
    public function userrefuse(){
        if(Request::instance()->isPost()){
            extract(input());
            $update = [
                'user_verify' => 3,
                'user_verify_remark' => $remark,
                'user_verify_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::name('u_user')
                ->where('user_id', $user_id)
                ->update($update);
            if($res){
                try {
                    $phone = Db::name('u_user')->where('user_id', $user_id)->value('user_phone');
                    YzxSms('590519',$remark,$phone,'');
                } catch (\Exception $e) {
                }
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $user_id = input('user_id');
        $this->assign('user_id',$user_id);
        return $this->fetch();
    }
    //----------------------------------------------------------------------------------------------------
    //------------------------------------企业审核----------------------------------------------------
    //----------------------------------------------------------------------------------------------------
    /**
     * 审核企业列表
     */
    public function company(){
        if(request()->isAjax()){
            extract(input());
            $where = [
                'c_status' => ['<>',3]
            ];
            if(isset($user_name) && $user_name != ''){
                $where['c_name|c_charge_name|c_charge_phone'] = ['like', '%' . $user_name . '%'];
            }
            if(isset($c_verify) && $c_verify != ''){
                $where['c_verify'] = $c_verify;
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_company')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_company')
                ->where($where)
                ->page($NowPage,$limits)
                ->order('c_verify asc,c_time asc')
                ->field('c_content',true)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    /**
     * 企业同意
     * @return \think\response\Json
     */
    public function comagree(){
        $id = input('id');
        $data = Db::table('up_company')->where('c_id',$id)->field('c_content',true)->find();
        $update = [
            'c_verify' => 2,
            'c_can' => 2,
            'c_name' => $data['c_name2'],
            'c_code' => $data['c_code2'],
            'c_zhizhao' => $data['c_zhizhao2'],
            'c_verifytime' => date('Y-m-d H:i:s')
        ];
        $res = Db::table('up_company')->where('c_id',$id)->update($update);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }

    /**
     * 企业拒绝
     * @return \think\response\Json
     */
    public function comrefuse(){
        if(Request::instance()->isPost()){
            extract(input());
            $update = [
                'c_verify' => 3,
                'c_verify_remark' => $remark,
                'c_verifytime' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_company')
                ->where('c_id',$c_id)
                ->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $c_id = input('c_id');
        $this->assign('c_id',$c_id);
        return $this->fetch();
    }
    /**
     * 企业介绍
     * @return \think\response\Json
     */
    public function comdesc(){
        $c_id = input('c_id');
        $res = Db::table('up_company')
            ->where('c_id',$c_id)
            ->value('c_content');
        $this->assign('content',$res);
        return $this->fetch();
    }
    //----------------------------------------------------------------------------------------------------
    //------------------------------------项目申请审核----------------------------------------------------
    //----------------------------------------------------------------------------------------------------
    /**
     * 审核列表
     */
    public function project(){
        if(request()->isAjax()){
            extract(input());
            $where = [
                'pro_del' => 1
            ];
            if(isset($user_name) && $user_name != ''){
                $where['pro_name|user_username|user_phone'] = ['like', '%' . $user_name . '%'];
            }
            if(isset($pu_status) && $pu_status != ''){
                $where['pu_status'] = $pu_status;
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_project_user')->alias('pu')
                ->join('up_project pro','pu.pu_pro_id=pro.pro_id','LEFT')
                ->join('up_u_user u','pu.pu_user_id=u.user_id','LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_project_user')->alias('pu')
                ->join('up_project pro','pu.pu_pro_id=pro.pro_id','LEFT')
                ->join('up_u_user u','pu.pu_user_id=u.user_id','LEFT')
                ->where($where)
                ->order('pu_status asc,pu_time asc')
                ->page($NowPage,$limits)
                ->field('pu_id,pu_status,pu_time,user_username,user_image,user_sex,user_phone,user_type,pro_name')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /**
     * 项目申请同意
     * @return \think\response\Json
     */
    public function proagree(){
        $id = input('id');
        $update = [
            'pu_status' => 2,
            'pu_update' => date('Y-m-d H:i:s')
        ];
        $res = Db::table('up_project_user')->where('pu_id',$id)->update($update);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }

    /**
     * 项目申请拒绝
     * @return \think\response\Json
     */
    public function prorefuse(){
        if(Request::instance()->isPost()){
            extract(input());
            $update = [
                'pu_status' => 3,
                'pu_remark' => $remark,
                'pu_update' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_project_user')
                ->where('pu_id',$pu_id)
                ->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $pu_id = input('pu_id');
        $this->assign('pu_id',$pu_id);
        return $this->fetch();
    }
}